package com.workflow.workflowproject.until;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.mybatisflex.core.query.QueryWrapper;
import com.workflow.workflowproject.entity.FlowForm;
import com.workflow.workflowproject.entity.dto.TaskInfoDto;
import com.workflow.workflowproject.entity.table.FlowFormTableDef;
import com.workflow.workflowproject.mapper.FlowFieldMapper;
import com.workflow.workflowproject.mapper.FlowFormMapper;
import jakarta.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 数据翻译组件，将提交上来的数据翻译成Label文字
 */
@Component
public class ShowInformation {

    @Autowired
    private FlowFormMapper flowFormMapper;
    @Autowired
    private FlowFieldMapper flowFieldMapper;

    private static ShowInformation showInformation;

    @PostConstruct
    public void init(){
        showInformation = this;
        showInformation.flowFormMapper = this.flowFormMapper;
        showInformation.flowFieldMapper = this.flowFieldMapper;
    }

    /**
     *
     * @param flowInfoId 流程定义ID
     * @param busKey 业务Key
     * @param userName 申请人名称
     * @param flowName 流程名称
     * @param createTime 创建时间
     * @param taskInfo 任务信息实体
     * @return
     */
    public static String setShowInformation(Integer flowInfoId, String busKey, String userName, String flowName, String createTime, JSONObject dataValue){
        //查询相应的表单数据
        QueryWrapper formQueryWrapper = new QueryWrapper();
        formQueryWrapper.where(FlowFormTableDef.FLOW_FORM.FLOW_INFO_ID.eq(flowInfoId));
        FlowForm flowForm = showInformation.flowFormMapper.selectOneByQuery(formQueryWrapper);
        //查询相应的字段数据
        List<JSONObject>  filedsJson = flowForm.getPcFormJson();
        HashMap<String,JSONObject> flowFieldHashMap = new HashMap<>();
        filedsJson.forEach(item->{
            flowFieldHashMap.put(item.getString("field"),item);
        });
        //值
        List<HashMap<String,Object>> dataMap = new ArrayList<>();
        HashMap<String,Object> valueBus = new HashMap<>();
        valueBus.put("title","申请编号");
        valueBus.put("type",0);
        valueBus.put("image",0);
        valueBus.put("value",busKey);
        dataMap.add(valueBus);
        HashMap<String,Object> valueFlowName = new HashMap<>();
        valueFlowName.put("title","申请名称");
        valueFlowName.put("type",0);
        valueFlowName.put("image",0);
        valueFlowName.put("value",flowName);
        dataMap.add(valueFlowName);
        HashMap<String,Object> valueSystemUser= new HashMap<>();
        valueSystemUser.put("title","申请人");
        valueSystemUser.put("type",0);
        valueSystemUser.put("image",0);
        valueSystemUser.put("value",userName);
        dataMap.add(valueSystemUser);
        HashMap<String,Object> valueTime = new HashMap<>();
        valueTime.put("title","申请时间");
        valueTime.put("type",0);
        valueTime.put("image",0);
        valueTime.put("value",createTime);
        dataMap.add(valueTime);
        //进行数据翻译
        for(String key : dataValue.keySet()){
            JSONObject flowFieldData = flowFieldHashMap.get(key);
            //判断是否存在此字段
            if(ObjectUtil.isNotEmpty(flowFieldData)){
                HashMap<String,Object> value = new HashMap<>();
                value.put("title",flowFieldData.getString("title"));
                //判断是不是自定义组件
                if(FieldEnum.getIsCustomComponentsType(flowFieldData.getString("type")).equals("YES")){
                    //自定义组件的话自行编写翻译
                }else{
                    //判断不是自定义组件,进行翻译
                    if(FieldEnum.getIsFileType(flowFieldData.getString("type")).equals("YES")){
                        //如果含有附件，则将附件信息进行保存（用于回显展示）
                        value.put("type",1);
                        value.put("image","");
                        value.put("value",dataValue.get(key));
                    }else{
                        if(FieldEnum.getOptionByElementType(flowFieldData.getString("type")).equals("YES")){
                            //判断是否有options
                            value.put("type",0);
                            value.put("image","");
                            //获取翻译
                            JSONArray optionJsonArray = flowFieldData.getJSONArray("options");
                            List<JSONObject> options = JSONArray.parseArray(optionJsonArray.toJSONString(),JSONObject.class);
                            for(JSONObject option : options){
                                if(option.get("value").toString().equals(dataValue.get(key))){
                                    value.put("value",option.get("label"));
                                    break;
                                }
                            }
                        }else{
                            value.put("type",0);
                            value.put("image","");
                            value.put("value",dataValue.get(key));
                        }

                    }
                }
                dataMap.add(value);
            }
        }
        return JSONObject.toJSONString(dataMap);
    }







}
